package com.sharefang.aproject.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.sharefang.aproject.adapter.XRecyclerViewAdapter;
import com.sharefang.aproject.bean.PptBean;
import com.sharefang.aproject.constant.FragmentType;
import com.sharefang.aproject.presenter.GetPptPresenter;
import com.sharefang.aproject.utils.UiUtil;
import com.sharefang.aproject.view.IPptView;

import java.util.List;

/**
 * Created by Administrator on 2016/8/12.
 */

@SuppressLint("ValidFragment")
public class ControlFragment extends RecyclerViewFragment implements IPptView {
    private int offset = -1;
    private GetPptPresenter pptPresenter = new GetPptPresenter(this);
    private Context context;

    public ControlFragment(Context context) {
        this.context = context;
        pptPresenter.getPpt(false, -1);
    }

    @Override
    void doRefresh() {
        offset = -1;
        pptPresenter.getPpt(false, offset);
    }

    @Override
    void doLoadMore() {
        offset += 11;
        pptPresenter.getPpt(true, offset);
    }

    @Override
    void setLayoutManager() {
        layoutManager = new GridLayoutManager(context, 2);
    }

    @Override
    void setAdapter() {
        adapter = new XRecyclerViewAdapter(context, FragmentType.CONTROL);
    }

    @Override
    void onItemClick(View view, int position) {

    }

    @Override
    void onItemLongClick(View view, int position) {

    }

    @Override
    public void doGetPpt(final List<PptBean> pptBeans, boolean loadMore) {
        if (loadMore) {
            //加载更多的操作
            if (pptBeans.size() != 0) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setPptData(pptBeans);
                        Logger.d("=================加载更多语句执行了");
                        adapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                    }
                }, 500);
            } else {
                xRecyclerView.setIsnomore(true);
            }
        } else {
            //刷新
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    adapter.resetPptData(pptBeans);
                    adapter.notifyDataSetChanged();
                    xRecyclerView.refreshComplete();
                    xRecyclerView.reset();
                }
            }, 500);
        }
    }

    @Override
    public void showFailedError(String message) {
        UiUtil.snake((Activity) context, message);
        xRecyclerView.reset();
    }
}
